package com.indexingsystem.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.indexingsystem.boss.Vo.AnalysisMasterVo;
import com.indexingsystem.boss.admin.mapper.AnalysisMasterMapper;
import com.indexingsystem.boss.common.PagedResult;
import com.indexingsystem.boss.entity.AnalysisMaster;
import com.indexingsystem.boss.utils.BeanUtils;
import com.indexingsystem.system.service.IAnalysisMasterService;

@Service
public class AnalysisMasterServiceImpl implements IAnalysisMasterService{
	
	@Resource
	private AnalysisMasterMapper analysisMasterMapper;

	@Override
	public int insertDayAnalysisMaster(AnalysisMaster record) {
		return analysisMasterMapper.insertDayAnalysisMaster(record);
	}

	@Override
	public int insertLatelyAnalysisMaster() throws Exception{
		// 1.获取24小时前的数据
		List<AnalysisMaster> analysisMaster = analysisMasterMapper.selectDayAnalysisMaster();
		// 2.添加最近行为日志信息
		analysisMasterMapper.insertLatelyAnalysisMaster(analysisMaster);
		int count = 0;
		for (AnalysisMaster info : analysisMaster) {
			// 3.删除当日行为日志信息
			count += analysisMasterMapper.deleteDayAnalysisMaster(info.getAnalysisId());
		}
		return count;
	}

	@Override
	public int insertHistoryAnalysisMaster()  throws Exception{
		// 1.获取7天前的数据
		List<AnalysisMaster> analysisMaster = analysisMasterMapper.selectLatelyAnalysisMaster();
		int count = 0;
		if (null != analysisMaster) {
			// 2.添加历史行为日志信息
			analysisMasterMapper.insertHistoryAnalysisMaster(analysisMaster);
			
			for (AnalysisMaster info : analysisMaster) {
				// 3.删除最近行为日志信息
				count += analysisMasterMapper.deleteLatelyAnalysisMaster(info.getAnalysisId());
			}
		}
		return count;
	}

	@Override
	public PagedResult<AnalysisMaster> getDayAnalysisMaster(AnalysisMasterVo analysisMasterVo) {
		Integer pageNo = analysisMasterVo.getPageNumber() == null ? 0 : analysisMasterVo.getPageNumber();
		Integer pageSize = analysisMasterVo.getPageSize() == null ? 10 : analysisMasterVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(analysisMasterMapper.getDayAnalysisMaster(analysisMasterVo));
	}

	@Override
	public PagedResult<AnalysisMaster> getLatelyAnalysisMaster(AnalysisMasterVo analysisMasterVo) {
		Integer pageNo = analysisMasterVo.getPageNumber() == null ? 0 : analysisMasterVo.getPageNumber();
		Integer pageSize = analysisMasterVo.getPageSize() == null ? 10 : analysisMasterVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(analysisMasterMapper.getLatelyAnalysisMaster(analysisMasterVo));
	}

	@Override
	public PagedResult<AnalysisMaster> getHistoryAnalysisMaster(AnalysisMasterVo analysisMasterVo) {
		Integer pageNo = analysisMasterVo.getPageNumber() == null ? 0 : analysisMasterVo.getPageNumber();
		Integer pageSize = analysisMasterVo.getPageSize() == null ? 10 : analysisMasterVo.getPageSize();
		// startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
		PageHelper.startPage(pageNo, pageSize);
		return BeanUtils.toPagedResult(analysisMasterMapper.getHistoryAnalysisMaster(analysisMasterVo));
	}
}